package com.thoughtworks.retailstore.fragments;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.google.gson.Gson;
import com.thoughtworks.models.CartModel;
import com.thoughtworks.models.CategoryModel;
import com.thoughtworks.models.ProductModel;
import com.thoughtworks.retailstore.R;
import com.thoughtworks.retailstore.adapters.ExpandableListAdapter;
import com.thoughtworks.retailstore.utils.Constants;

// TODO: Auto-generated Javadoc
/**
 * The Class PlaceHolderFragment.
 */
public class PlaceHolderFragment extends Fragment 
{

	/** The exp list view. */
	private ExpandableListView expListView;

	/** The adapter. */
	private ExpandableListAdapter adapter ;

	/** The shared pref. */
	private SharedPreferences sharedPref ;

	/** The editor. */
	private SharedPreferences.Editor editor ;

	/** The gson. */
	private Gson gson ;

	/**
	 * New instance.
	 *
	 * @return the place holder fragment
	 */
	public static PlaceHolderFragment newInstance()
	{
		PlaceHolderFragment frag = new PlaceHolderFragment();
		return frag ;
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) 
	{
		View view = inflater.inflate(R.layout.fragment_expandable_list_layout, null);

		initialize(view);

		prepareListData();

		adapter = new ExpandableListAdapter(getActivity(), Constants.categoryList);
		expListView.setAdapter(adapter);

		//group click listener
		expListView.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {

				return false;
			}
		});

		//child click listener
		expListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {

				ProductDetailsFragment frag = ProductDetailsFragment.
						newInstance(groupPosition, childPosition);
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				ft.replace(R.id.container,frag,"product_details_fragment");
				ft.addToBackStack("product_details_fragment");
				ft.commit();

				return false;
			}
		});

		return view;
	}

	/*
	 * method that initializes the view  
	 */

	/**
	 * Initialize.
	 *
	 * @param view the view
	 */
	public void initialize(View view)
	{
		expListView = (ExpandableListView)view.findViewById(R.id.expListView);
		sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
		editor = sharedPref.edit();
		gson = new Gson();
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onResume()
	 */
	@Override
	public void onResume() {

		super.onResume();
		convertToObject();
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onPause()
	 */
	@Override
	public void onPause() {
		convertToJsonStr();
		super.onPause();
	}

	/**
	 * method that converts object to json string.
	 */
	public void convertToJsonStr()
	{
		try{
			String jsonStr = gson.toJson(CartModel.getInstance());
			editor.putString(Constants.CART_OBJECT_KEY, jsonStr);
			editor.commit();
		}
		catch(Exception e){
			e.printStackTrace();
		}

	}

	/**
	 * method that converts json string to object.
	 */
	public void convertToObject()
	{
		try{
			if (CartModel.cart == null) {
				String json = sharedPref.getString(Constants.CART_OBJECT_KEY, "empty");
				if (!json.equals("empty")) 
				{
					CartModel.setPersistentObj(gson.fromJson(json, CartModel.class));
				}
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * method that prepares list data,
	 * that is used to displays list.
	 */
	public void prepareListData()
	{
		try{
			CategoryModel categEle = new CategoryModel();
			CategoryModel categFur = new CategoryModel();
			Constants.cartList = new ArrayList<CartModel>();

			categEle.setCategName(Constants.CT_ELECTRONICS);
			categFur.setCategName(Constants.CT_FURNITURE);

			ArrayList<ProductModel> e_prodList = new ArrayList<ProductModel>();
			ArrayList<ProductModel> f_prodList = new ArrayList<ProductModel>();

			Constants.categoryList = new ArrayList<CategoryModel>();

			ProductModel pmo = new ProductModel();
			pmo.setProductCategory(Constants.CT_ELECTRONICS);
			pmo.setProductName(Constants.PR_MICROWAVE);
			pmo.setProductPrice(Constants.elecPriceArray[0]);
			pmo.setProductImgId(Constants.electronicsImgIds[0]);
			pmo.setCategId(0);
			pmo.setProdId(0);

			ProductModel ptv = new ProductModel();
			ptv.setProductCategory(Constants.CT_ELECTRONICS);
			ptv.setProductName(Constants.PR_TV);
			ptv.setProductPrice(Constants.elecPriceArray[1]);
			ptv.setProductImgId(Constants.electronicsImgIds[1]);
			ptv.setCategId(0);
			ptv.setProdId(1);


			ProductModel pvc = new ProductModel();
			pvc.setProductCategory(Constants.CT_ELECTRONICS);
			pvc.setProductName(Constants.PR_VACUMNCLR);
			pvc.setProductPrice(Constants.elecPriceArray[2]);
			pvc.setProductImgId(Constants.electronicsImgIds[2]);
			pvc.setCategId(0);
			pvc.setProdId(2);

			ProductModel ptb = new ProductModel();
			ptb.setProductCategory(Constants.CT_FURNITURE);
			ptb.setProductName(Constants.PR_TABLE);
			ptb.setProductPrice(Constants.furnPriceArray[0]);
			ptb.setProductImgId(Constants.furnitureImgIds[0]);
			ptb.setCategId(1);
			ptb.setProdId(0);

			ProductModel pch = new ProductModel();
			pch.setProductCategory(Constants.CT_FURNITURE);
			pch.setProductName(Constants.PR_CHAIR);
			pch.setProductPrice(Constants.furnPriceArray[1]);
			pch.setProductImgId(Constants.furnitureImgIds[1]);
			pch.setCategId(1);
			pch.setProdId(1);

			ProductModel pal = new ProductModel();
			pal.setProductCategory(Constants.CT_FURNITURE);
			pal.setProductName(Constants.PR_ALMIRAH);
			pal.setProductPrice(Constants.furnPriceArray[2]);
			pal.setProductImgId(Constants.furnitureImgIds[2]);
			pal.setCategId(1);
			pal.setProdId(2);

			e_prodList.add(pmo);
			e_prodList.add(ptv);
			e_prodList.add(pvc);

			f_prodList.add(ptb);
			f_prodList.add(pch);
			f_prodList.add(pal);

			categEle.setProdList(e_prodList);
			categFur.setProdList(f_prodList);

			Constants.categoryList.add(categEle);
			Constants.categoryList.add(categFur);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

}
